package io.renren.modules.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.exception.RRException;
import io.renren.common.utils.JacksonUtils;
import io.renren.common.utils.PageData;
import io.renren.modules.edu.dao.TEduSalaryDao;
import io.renren.modules.edu.dto.TEduSalaryDTO;
import io.renren.modules.edu.entity.TEduSalaryEntity;
import io.renren.modules.edu.service.TEduSalaryService;
import io.renren.modules.edu.service.TEduTeacherinfoService;
import io.renren.modules.edu.vo.TEduSalaryVO;
import io.renren.modules.edu.vo.TEduTeacherinfoVO;
import io.renren.modules.security.SecurityUser;
import io.renren.modules.security.UserDetail;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 薪资表
 *
 * @author zwp zwp
 * @since 1.0.0 2022-07-11
 */
@Slf4j
@Service
public class TEduSalaryServiceImpl extends ServiceImpl<TEduSalaryDao, TEduSalaryEntity> implements TEduSalaryService {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private TEduTeacherinfoService tEduTeacherinfoService;

    @Override
    public void save(TEduSalaryDTO tEduSalaryDTO) {
        TEduSalaryEntity tEduSalaryEntity = JacksonUtils.obj2pojo(tEduSalaryDTO, TEduSalaryEntity.class);
        this.baseMapper.insert(tEduSalaryEntity);
    }

    @Override
    public void update(TEduSalaryDTO tEduSalaryDTO) {
        TEduSalaryEntity tEduSalaryEntity = JacksonUtils.obj2pojo(tEduSalaryDTO, TEduSalaryEntity.class);
        this.updateById(tEduSalaryEntity);
    }

    @Override
    public PageData<TEduSalaryVO> querySalaryPage(Map<String, Object> params) {
        log.info("获取对应用户的薪资表");
        // 获取当前用户
        UserDetail user = SecurityUser.getUser();
        // 获取请求对象(用于分页筛选条件)
        String page = (String) params.get("page");
        String limit = (String) params.get("limit");
        // 获取起始长度
        Integer size = Integer.valueOf(limit);
        Integer index = Integer.valueOf(page);
        // 创建分页对象
        IPage<TEduSalaryEntity> iPage = new Page<>(index, size);
        QueryWrapper<TEduSalaryEntity> wrapper = new QueryWrapper<TEduSalaryEntity>();
        wrapper.eq("sys_user_id",user.getId());
        this.baseMapper.selectPage(iPage, wrapper);

        List<TEduSalaryVO> tEduSalaryVOList = JacksonUtils.listObj2pojo(iPage.getRecords(), TEduSalaryVO.class);
        tEduSalaryVOList.forEach(e -> {
            e.setRealName(user.getRealName());
        });
        return new PageData<TEduSalaryVO>(tEduSalaryVOList, iPage.getTotal());
    }

    @Override
    public void confirmSalary(Long id) {
        log.info("确认薪资");
        TEduSalaryEntity tEduSalaryEntity = new TEduSalaryEntity();
        tEduSalaryEntity.setId(id);
        tEduSalaryEntity.setStatus(1);
        this.baseMapper.updateById(tEduSalaryEntity);
    }

    @Override
    public PageData<TEduSalaryVO> querySalaryByYearMonthPage(Map<String, Object> params) {
        log.info("获取对应年份、月份 用户的薪资表");
        // 获取请求对象(用于分页筛选条件)
        String page = (String) params.get("page");
        String limit = (String) params.get("limit");
        String year = (String) params.get("year");
        String month = (String) params.get("month");
        // 获取起始长度
        Integer size = Integer.valueOf(limit);
        Integer index = Integer.valueOf(page);

        // 获取所有的用户信息id集合
        List<SysUserEntity> sysUserEntityList = sysUserService.getBaseMapper().selectList(null);
        List<Long> sysUserIdList = sysUserEntityList.stream().map(e -> e.getId()).collect(Collectors.toList());
        // 创建分页对象
        IPage<TEduSalaryEntity> iPage = new Page<>(index, size);
        QueryWrapper<TEduSalaryEntity> wrapper = new QueryWrapper<TEduSalaryEntity>();
        wrapper.in("sys_user_id", sysUserIdList);
        wrapper.eq("year", year);
        wrapper.eq("month", month);
        this.baseMapper.selectPage(iPage, wrapper);

        List<TEduSalaryVO> tEduSalaryVOList = JacksonUtils.listObj2pojo(iPage.getRecords(), TEduSalaryVO.class);
        tEduSalaryVOList.forEach(e -> {
            SysUserEntity sysUserEntity = sysUserService.getBaseMapper().selectById(e.getSysUserId());
            e.setRealName(sysUserEntity.getRealName());
        });
        return new PageData<TEduSalaryVO>(tEduSalaryVOList, iPage.getTotal());
    }

    @Override
    public void onSubmit(TEduSalaryDTO dto) {
        log.info("薪资录入");
        String realName = dto.getRealName();
        String mobile = dto.getMobile();
        SysUserEntity sysUserEntity = sysUserService.queryUser(realName, mobile);
        if (sysUserEntity == null) {
            throw new RRException("该用户不存在！薪资无法录入！");
        }
        TEduTeacherinfoVO tEduTeacherinfoVO = tEduTeacherinfoService.queryTeacherBySysUserId(sysUserEntity.getId());
        if (tEduTeacherinfoVO == null) {
            throw new RRException("该用户教师信息未登记！薪资无法录入！");
        }
        // 插入薪资数据
        TEduSalaryEntity tEduSalaryEntity = JacksonUtils.obj2pojo(dto, TEduSalaryEntity.class);
        tEduSalaryEntity.setSysUserId(sysUserEntity.getId());
        tEduSalaryEntity.setEduTeacherinfoId(tEduTeacherinfoVO.getId());
        // 实发工资
        BigDecimal netSalary = dto.getBasicSalary().add(dto.getPostSalary()).add(dto.getMealSalary()).add(dto.getTravelSalary()).add(dto.getOvertimeSalary());
        tEduSalaryEntity.setNetSalary(netSalary);
        this.baseMapper.insert(tEduSalaryEntity);
    }


}
